/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.13
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   等待指令属性编辑视窗
*                                                                         *
 ***************************************************************************/
#include "waitinstructiondelegate.h"
#include <QDebug>

WaitInstructionDelegate::WaitInstructionDelegate(QWidget *parent):
    QObject(parent)
{
}

void WaitInstructionDelegate::initialView()
{
    waitInstructionView->waitInstruction = "WAIT ";
    waitInstructionView->waitType = EM_WAIT_INSTRUCTION_TIME;
    waitInstructionView->waitTime = 0.0;
    waitInstructionView->rIndex = "1";
    waitInstructionView->waitCondition = "DI[1]=ON";
    waitInstructionView->note = "";
    waitInstructionView->timeoutSelectedFlag = 0;
    waitInstructionView->timeoutLblIndexType = 0;
    waitInstructionView->timeoutLblIndex = 1;

    waitInstructionView->limitWaitTimeSelectFlag = 0;
    waitInstructionView->limitWaitTime = 0.0;
    waitInstructionView->timeoutRIndex = "1";
    waitInstructionView->limitWaitTimeType = 0;

    //waitInstructionView->updateView();
}

void WaitInstructionDelegate::selectWaitTimeSlot()
{
    waitInstructionView->waitType = EM_WAIT_INSTRUCTION_TIME;
    waitInstructionView->updateView();
    qDebug("select wait time");
}

void WaitInstructionDelegate::selectWaitRSlot()
{
    waitInstructionView->waitType = EM_WAIT_INSTRUCTION_R;
    waitInstructionView->updateView();
    qDebug("select wait R");
}

void WaitInstructionDelegate::selectWaitConditionSlot()
{
    waitInstructionView->waitType = EM_WAIT_INSTRUCTION_CONDITION;
    waitInstructionView->updateView();
    qDebug("select wait condition");
}

void WaitInstructionDelegate::clearNoteSlot()
{
    waitInstructionView->note.clear();

    qDebug("clear note");
}

void WaitInstructionDelegate::finishEditInstructionSlot()
{
    waitInstructionView->waitInstruction = "WAIT ";
    if(EM_WAIT_INSTRUCTION_TIME == waitInstructionView->waitType)
    {
        waitInstructionView->waitInstruction += QString("%1").arg(waitInstructionView->waitTime) + "sec";
    }
    else if(EM_WAIT_INSTRUCTION_R == waitInstructionView->waitType)
    {
        waitInstructionView->waitInstruction = waitInstructionView->waitInstruction + "R"
                + "[" + waitInstructionView->rIndex + "]";
    }
    else if(EM_WAIT_INSTRUCTION_CONDITION == waitInstructionView->waitType)
    {
        waitInstructionView->waitInstruction += waitInstructionView->waitCondition;
        if(1 == waitInstructionView->timeoutSelectedFlag)
        {
            waitInstructionView->waitInstruction += ",TIMEOUT LBL[";
            if(0 == waitInstructionView->timeoutLblIndexType)
            {
                waitInstructionView->waitInstruction += QString::number(waitInstructionView->timeoutLblIndex) + "]";
            }
            else if(1 == waitInstructionView->timeoutLblIndexType)
            {
                waitInstructionView->waitInstruction += "R[" + QString::number(waitInstructionView->timeoutLblIndex) + "]]";
            }
            if(1 == waitInstructionView->limitWaitTimeSelectFlag)
            {
                if(0 == waitInstructionView->limitWaitTimeType)
                {
                    waitInstructionView->waitInstruction += " " + QString("%1").arg(waitInstructionView->limitWaitTime) + "sec";
                }
                else if(1 == waitInstructionView->limitWaitTimeType)
                {
                    waitInstructionView->waitInstruction = waitInstructionView->waitInstruction + " R[" + waitInstructionView->timeoutRIndex + "]";
                }
            }
        }
    }

    waitInstructionView->waitInstruction = waitInstructionView->waitInstruction + "; " + "//" + waitInstructionView->note;
}

void WaitInstructionDelegate::isSelectTimeoutSlot()
{
    if(0 == waitInstructionView->timeoutSelectedFlag)
    {
        waitInstructionView->timeoutSelectedFlag = 1;
    }
    else if(1 == waitInstructionView->timeoutSelectedFlag)
    {
        waitInstructionView->timeoutSelectedFlag = 0;
    }
    waitInstructionView->updateView();
}

void WaitInstructionDelegate::isSelectSettingLimitTimeSlot()
{
    if(0 == waitInstructionView->limitWaitTimeSelectFlag)
    {
        waitInstructionView->limitWaitTimeSelectFlag = 1;
    }
    else if(1 == waitInstructionView->limitWaitTimeSelectFlag)
    {
        waitInstructionView->limitWaitTimeSelectFlag = 0;
    }
    waitInstructionView->updateView();
}

